﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KMFrame.Permission.Util;
using LGCourt.Logic.BLL;
using LGCourt.Logic.NBL.DBEntities;

namespace lgweb.user
{
    public partial class EvidenceFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!SessionBox.CheckUserSession())
            {
                Response.Redirect("/user/login.aspx");
            }
            DownFil();
        }

        private void DownFil()
        {
            int id = 0;
            string pID = string.IsNullOrEmpty(Request.QueryString["id"]) ? "0" : Request.QueryString["id"];
            int.TryParse(pID, out id);
            CaseInfo bll = new CaseInfo();
            pZJJHB zjjhb = bll.GetZJJHXX(id, SessionBox.GetUserSession().UserID, SessionBox.GetUserSession().UserName);
            if (zjjhb == null)
                return;
            string fileName = string.IsNullOrEmpty(zjjhb.ZJWJM) ? zjjhb.ZJMC : zjjhb.ZJWJM;
            string filePath = "~//file//zhengjujiaohuan//" + fileName;
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

            if (fileInfo.Exists == true)
            {
                const long ChunkSize = 102400;
                byte[] buffer = new byte[ChunkSize];

                Response.Clear();
                System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
                long dataLengthToRead = iStream.Length;
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(zjjhb.ZJMC));
                while (dataLengthToRead > 0 && Response.IsClientConnected)
                {
                    int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
                    Response.OutputStream.Write(buffer, 0, lengthRead);
                    Response.Flush();
                    dataLengthToRead = dataLengthToRead - lengthRead;
                }
                Response.Close();
            }
            else
            {
                if (zjjhb.ZJNR == null)
                    return;
                const int ChunkSize = 102400;
                byte[] buffer = new byte[ChunkSize];

                Response.Clear();
                int beginLen = 0;
                int endLen = ChunkSize < zjjhb.ZJNR.Length ? ChunkSize : zjjhb.ZJNR.Length;
               
                long dataLengthToRead = zjjhb.ZJNR.Length;
                Response.ContentType = string.IsNullOrEmpty(zjjhb.ZJLX) ? "application/octet-stream" : zjjhb.ZJLX;
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
                while (endLen <= zjjhb.ZJNR.Length && Response.IsClientConnected)
                {
                   
                    //int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
                    //zjjhb.ZJNR.
                    Response.OutputStream.Write(zjjhb.ZJNR, beginLen, endLen);
                    Response.Flush();
                    if (endLen == zjjhb.ZJNR.Length)
                        break;
                    beginLen += ChunkSize;
                    endLen += ChunkSize;
                    
                    endLen = endLen < zjjhb.ZJNR.Length ? endLen : zjjhb.ZJNR.Length;
                }
                Response.Close();
            }


        }
    }
}